﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EBOOK.admin
{
    public partial class login : System.Web.UI.Page
    {
        private COM.DTO.User user = new COM.DTO.User();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[COM.CONST.SESSION.USER] == null)
            {

            }
            else
            {
                user = (COM.DTO.User)Session[COM.CONST.SESSION.USER];
                if (user.Role.RoleId <= COM.CONST.ROLE.HOTELOWNER) // Role higher
                {
                    Response.Redirect(COM.CONST.PAGE.ADMIN_INDEX);
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string txtUser = txtUsername.Text;
            string txtPass = txtPassword.Text;
            COM.DTO.User User = BUS.UserHandler.Login(txtUser, txtPass);
            if (User == null)
            {
                Response.Write("<script> alert('login failed !!!')</script>");
            }
            else
            {
                if (User.Username == txtUser && User.Password == COM.Encrypt.EncodePassword(txtPass))
                {
                    Session[COM.CONST.SESSION.USER] = User;
                    Response.Redirect(COM.CONST.PAGE.ADMIN_INDEX);
                }
                else Response.Write("<script> alert('login failed !!!')</script>");
            }
        }
    }
}